package com.yinhai.message.manage.sysapp.bpo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.config.impl.SysConfig;
import com.yinhai.core.service.ta3.domain.service.TaBaseService;
import com.yinhai.message.core.po.MessageAppManagePo;
import com.yinhai.message.manage.sysapp.dao.ISysAppDao;
import com.yinhai.message.manage.sysapp.vo.SysAppVo;


public class SysAppBpoImpl extends TaBaseService implements ISysAppBpo {

    @Resource(name = "sysAppDao")
    private ISysAppDao sysAppDao;

    @Override
    public String getSequeue() {
        return getStringSeq("SEQ_MESSAGE");
    }

    @Override
    public void addSysApp(MessageAppManagePo po) {
        sysAppDao.saveSysApp(po);
    }

    @Override
    public void modifySysApp(MessageAppManagePo po) {
        sysAppDao.updateSysApp(po);
    }

    @Override
    public IPage<SysAppVo> getSysAppList(Map<String, Object> param, int start, int limit, String gridId) {
        Map<String, Object> param1 = new HashMap<>();
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.sysapp.vo.SysAppVo(")
                .append("mp.id,")
                .append("mp.systemid,")
                .append("mp.systemname,")
                .append("mp.userid,")
                .append("u.name,")
                .append("mp.sendertypeorder,")
                .append("mp.sendertypeorderdesc,")
                .append("mp.ticket")
                .append(") from MessageAppManagePo mp ,")
                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append("  u")
                .append(" where 1=1 and u.userid = mp.userid");
        Object systemid = param.get("systemid");
        Object systemname = param.get("systemname");
        if (!StringUtils.isEmpty(systemid)) {
            hql.append(" and mp.systemid =:systemid");
            param1.put("systemid", systemid);
        }
        if (!StringUtils.isEmpty(systemname)) {
            hql.append(" and mp.systemname like :systemname");
            param1.put("systemname", "%" + systemname + "%");
        }
        return sysAppDao.queryList(hql.toString(), param1, start, limit, gridId);
    }

    @Override
    public List<SysAppVo> getSysAppList() {
        StringBuffer hql = new StringBuffer();
        hql.append("select distinct new com.yinhai.message.manage.sysapp.vo.SysAppVo(")
                .append("mp.id,")
                .append("mp.systemid,")
                .append("mp.systemname,")
                .append("mp.sendertypeorder")
                .append(") from MessageAppManagePo mp ");
        return sysAppDao.queryList(hql.toString());
    }

    @Override
    public SysAppVo getSysAppById(String id) {
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.sysapp.vo.SysAppVo(")
                .append("mp.id,")
                .append("mp.systemid,")
                .append("mp.systemname,")
                .append("mp.userid,")
                .append("u.name,")
                .append("mp.sendertypeorder,")
                .append("mp.sendertypeorderdesc,")
                .append("mp.ticket")
                .append(") from MessageAppManagePo mp ,")
                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append("  u")
                .append(" where 1=1 and u.userid = mp.userid and mp.id = ?");
        return sysAppDao.queryOneById(hql.toString(), id);
    }

    @Override
    public Long getCountBySysId(String systemId, String id) {
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.sysapp.vo.SysAppVo(")
                .append("mp.id,")
                .append("mp.systemid,")
                .append("mp.systemname,")
                .append("mp.userid,")
                .append("u.name,")
                .append("mp.sendertypeorder,")
                .append("mp.sendertypeorderdesc,")
                .append("mp.ticket")
                .append(") from MessageAppManagePo mp ,")
                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append("  u")
                .append(" where 1=1 and u.userid = mp.userid and mp.systemid = ?");
        if (!StringUtils.isEmpty(id)) {
            hql.append(" and mp.id != ?");
        }
        return sysAppDao.queryCountBySysId(hql.toString(), systemId, id);
    }

    @Override
    public SysAppVo getVoBySystemId(String systemId) {
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.sysapp.vo.SysAppVo(")
                .append("mp.ticket,")
                .append("mp.sendertypeorder,")
                .append("mp.masterSecret")
                .append(") from MessageAppManagePo mp")
                .append(" where mp.systemid = ? and rownum<=1");
        return sysAppDao.queryVoBySystemId(hql.toString(), systemId);
    }

    @Override
    public void dropSysApp(MessageAppManagePo po) {
        sysAppDao.deleteSysApp(po);
    }
}
